//index.js
//获取应用实例
var cfg = require('../../utils/cfg');
var util = require('../../utils/util');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
var common = require('../../utils/common');
var sendRequest = require('../../utils/requestUtils');

Page({
  data: {
    areas : [],
    selAreaId : '-1',
    merhcantName : '',
    phone : '',
    pictureUrl : '',
    applyButtonHidden : true,
    tempFilePaths : []
  },
  onLoad: function () {
    let that = this;
    that.loadAreas();
  },

 //提交申请
  shenqing : function() {
    let that = this;
    if (that.data.merhcantName == '' || that.data.merhcantName==undefined) {
      util.alert('请输入店铺名称');
      return;
    }

    if (that.data.phone == '' || that.data.phone == undefined) {
      util.alert('请输入手机号码');
      return;
    }

    if (that.data.selAreaId == '-1' || that.data.selAreaId == undefined) {
      util.alert('请选择店铺所在地点');
      return;
    }

    wx.uploadFile({
      url: cfg.host + cfg.djurl.shenqing,
      filePath: that.data.tempFilePaths[0],
      name: 'file',
      formData: {
        'merchantName': that.data.merhcantName,
        'pictureName': that.data.merhcantName,
        'areaId': that.data.selAreaId,
        'phone': that.data.phone,
        'openId' : 'xxx'
      },
      success(res) {
        let result = JSON.parse(res.data);
        if (result.code !== 1000) {
          util.alert(result.message);
          return;
        }

        //申请成功...
        console.log('申请成功');
        wx.reLaunch({
          url: '/pages/home/home'
        })
        
      }
    })

  },

  loadAreas : function() {
    let that = this;
    sendRequest.sendReq(cfg.djurl.loadAreas, {
    }, function(result) {
       console.log('>>>>>>areas=' + result.data.data.length);
      that.setData({
        areas: result.data.data
      });

      console.log(that.data.areas);
    });
  },

  change : function(result) {
    console.log(result.detail);
    this.data.selAreaId = result.detail.id;
  },

  choseImg : function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        that.data.tempFilePaths = res.tempFilePaths;
        console.log('>>>res.tempFilePaths=' + res.tempFilePaths[0]);
        that.setData({
          pictureUrl: res.tempFilePaths[0],
          applyButtonHidden : false
        });
      }
    })
  },

  merchantNameChange: function (event) {
    this.data.merhcantName = event.detail.detail.value;
  },

  phoneChange: function (event) {
    // let pages = getCurrentPages();
    // let currentPage = pages[pages.length - 1];
    this.data.phone = event.detail.detail.value;
  }

})
